package com.hsjk.szwj.module.pay.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hsjk.szwj.framework.common.constant.CommonConstant;
import com.hsjk.szwj.module.pay.dal.dataobject.config.IsvConfigContext;
import com.hsjk.szwj.module.pay.dal.dataobject.config.IsvParams;
import com.hsjk.szwj.module.pay.dal.dataobject.config.IsvsubMchParams;
import com.hsjk.szwj.module.pay.dal.dataobject.config.MchAppConfigContext;
import com.hsjk.szwj.module.pay.dal.dataobject.config.NormalMchParams;
import com.hsjk.szwj.module.pay.dal.dataobject.config.WxServiceWrapper;
import com.hsjk.szwj.module.pay.dal.dataobject.config.WxpayIsvParams;
import com.hsjk.szwj.module.pay.dal.dataobject.config.WxpayNormalMchParams;
import com.hsjk.szwj.module.pay.dal.dataobject.system.IsvInfoDO;
import com.hsjk.szwj.module.pay.dal.dataobject.system.MchAppDO;
import com.hsjk.szwj.module.pay.dal.dataobject.system.MchInfoDO;
import com.hsjk.szwj.module.pay.dal.dataobject.system.PayInterfaceConfigDO;
import com.hsjk.szwj.module.pay.enums.IfCodeEnum;
import com.hsjk.szwj.module.pay.enums.MchInfoTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author chenjiawei
 * @desc 配置信息查询服务 （兼容 缓存 和 直接查询方式）
 * @Date 2023/08/21 16:06
 **/
@Slf4j
@Service
public class ConfigContextQueryService {
    @Resource
    private ConfigContextService configContextService;
    @Resource
    private IMchInfoService mchInfoService;
    @Resource
    private IMchAppService mchAppService;
    @Resource
    private PayInterfaceConfigService payInterfaceConfigService;
    @Resource
    private PayInterfaceDefineService payInterfaceDefineService;
    @Resource
    private IIsvInfoService iIsvInfoService;

    private boolean isCache() {
        return false;
    }

    public MchAppDO queryMchApp(Long mchId, Long mchAppId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchId, mchAppId).getMchApp();
        }

        return mchAppService.getOneByMchIdAndId(mchId, mchAppId);
    }

    public MchAppConfigContext queryMchInfoAndAppInfo(Long mchAppId) {
        MchAppDO mchAppDO = mchAppService.getById(mchAppId);
        MchInfoDO mchInfoDO = mchInfoService.getMchInfoById(mchAppDO.getMchId());
        return queryMchInfoAndAppInfo(mchInfoDO.getId(), mchAppId);
    }

    public MchAppConfigContext queryMchInfoAndAppInfo(Long mchId, Long mchAppId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchId, mchAppId);
        }

        MchInfoDO mchInfo = mchInfoService.getMchInfoById(mchId);
        MchAppDO mchApp = queryMchApp(mchId, mchAppId);

        if (mchInfo == null || mchApp == null) {
            return null;
        }

        MchAppConfigContext result = new MchAppConfigContext();
        result.setMchInfo(mchInfo);
        result.setMchId(mchId);
        result.setMchType(mchInfo.getMchType());

        result.setMchApp(mchApp);
        result.setAppId(mchAppId);

        return result;
    }


    public NormalMchParams queryNormalMchParams(Long mchId, Long mchAppId, Long ifId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchId, mchAppId)
                    .getNormalMchParamsByIfId(ifId);
        }
        String ifCode = payInterfaceDefineService.getCodeById(ifId);
        // 查询商户的所有支持的参数配置
        PayInterfaceConfigDO payInterfaceConfig =
                payInterfaceConfigService.getOne(new LambdaQueryWrapper<PayInterfaceConfigDO>()
                        .select(PayInterfaceConfigDO::getIfId, PayInterfaceConfigDO::getIfParams)
                        .eq(PayInterfaceConfigDO::getState, CommonConstant.YES)
                        .eq(PayInterfaceConfigDO::getInfoType, MchInfoTypeEnum.APP.getType())
                        .eq(PayInterfaceConfigDO::getInfoId, mchAppId)
                        .eq(PayInterfaceConfigDO::getIfId, ifId)
                );

        if (payInterfaceConfig == null) {
            return null;
        }

        return NormalMchParams.factory(ifCode,
                payInterfaceConfig.getIfParams());
    }


    public IsvsubMchParams queryIsvsubMchParams(Long mchId, Long mchAppId, Long ifId) {

        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchId, mchAppId)
                    .getIsvsubMchParamsByIfId(ifId);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfigDO payInterfaceConfig =
                payInterfaceConfigService.getOne(new LambdaQueryWrapper<PayInterfaceConfigDO>()
                        .select(PayInterfaceConfigDO::getIfId, PayInterfaceConfigDO::getIfParams)
                        .eq(PayInterfaceConfigDO::getState, CommonConstant.YES)
                        .eq(PayInterfaceConfigDO::getInfoType, MchInfoTypeEnum.APP.getType())
                        .eq(PayInterfaceConfigDO::getInfoId, mchAppId)
                        .eq(PayInterfaceConfigDO::getIfId, ifId)
                );

        if (payInterfaceConfig == null) {
            return null;
        }
        String ifCode = payInterfaceDefineService.getCodeById(payInterfaceConfig.getIfId());
        return IsvsubMchParams.factory(ifCode,
                payInterfaceConfig.getIfParams());
    }


    public IsvParams queryIsvParams(Long isvId, Long ifId) {

        if (isCache()) {
            IsvConfigContext isvConfigContext =
                    configContextService.getIsvConfigContext(isvId);
            return isvConfigContext == null ? null :
                    isvConfigContext.getIsvParamsByIfId(ifId);
        }

        // 查询商户的所有支持的参数配置
        PayInterfaceConfigDO payInterfaceConfig =
                payInterfaceConfigService.getOne(new LambdaQueryWrapper<PayInterfaceConfigDO>()
                        .select(PayInterfaceConfigDO::getIfId, PayInterfaceConfigDO::getIfParams)
                        .eq(PayInterfaceConfigDO::getState, CommonConstant.YES)
                        .eq(PayInterfaceConfigDO::getInfoType, MchInfoTypeEnum.ISV.getType())
                        .eq(PayInterfaceConfigDO::getInfoId, isvId)
                        .eq(PayInterfaceConfigDO::getIfId, ifId)
                );

        if (payInterfaceConfig == null) {
            return null;
        }
        String ifCode = payInterfaceDefineService.getCodeById(payInterfaceConfig.getIfId());
        return IsvParams.factory(ifCode,
                payInterfaceConfig.getIfParams());

    }


    public WxServiceWrapper getWxServiceWrapper(MchAppConfigContext mchAppConfigContext) {
        log.info("getWxServiceWrapper:{}", mchAppConfigContext);
        if (isCache()) {
            return configContextService.getMchAppConfigContext(mchAppConfigContext.getMchId(),
                    mchAppConfigContext.getAppId()).getWxServiceWrapper();
        }
        Long ifId = payInterfaceDefineService.getIdByCode(IfCodeEnum.WXPAY.getCode());
        if (mchAppConfigContext.isIsvsubMch()) {
            IsvInfoDO isvInfoDO =
                    iIsvInfoService.getIsvInfoById(mchAppConfigContext.getMchInfo().getIsvId());
            WxpayIsvParams wxParams = (WxpayIsvParams)
                    queryIsvParams(isvInfoDO.getId(), ifId);
            return WxServiceWrapper.buildWxServiceWrapper(wxParams);
        } else {

            WxpayNormalMchParams wxParams = (WxpayNormalMchParams)
                    queryNormalMchParams(mchAppConfigContext.getMchId(),
                            mchAppConfigContext.getAppId(),
                            ifId);
            return WxServiceWrapper.buildWxServiceWrapper(wxParams);
        }

    }
}
